package com.netease.financial.module.main;

import android.support.annotation.NonNull;

import com.netease.financial.common.util.NfsLog;
import com.netease.financial.common.util.PackageUtils;
import com.netease.financial.common.util.preferences.AppUpdatePreferencesHelper;
import com.netease.financial.domain.interactor.DefaultSubscriber;
import com.netease.financial.domain.interactor.UseCase;
import com.netease.financial.domain.model.ActivityPage;
import com.netease.financial.domain.model.AppUpdate;
import com.netease.financial.domain.model.Logout;
import com.netease.financial.internal.di.ActivityScope;
import com.netease.financial.mapper.NfsViewModelDataMapper;
import com.netease.financial.module.activitypage.AdvertisementPreferencesHelper;
import com.netease.financial.presenter.Presenter;
import com.netease.financial.viewmodel.AppUpdateViewModel;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * {@link Presenter} that controls communication between views and models of the presentation
 * layer.
 */
@ActivityScope
public class MainPresenterImpl implements MainPresenter {

    private static final String TAG = MainPresenterImpl.class.getSimpleName();

    private MainView mainView;

    private final UseCase getUpdateDetailUseCase;
    private final UseCase logoutUserCase;
    private final UseCase activityPageUseCase;

    private final NfsViewModelDataMapper nfsViewModelDataMapper;

    @Inject
    public MainPresenterImpl(@Named("app_update_info") UseCase getUpdateDetailUseCase,
                             @Named("logout") UseCase logoutUseCase,
                             @Named("activity_page") UseCase activityPageUseCase,
                             NfsViewModelDataMapper nfsViewModelDataMapper) {
        this.getUpdateDetailUseCase = getUpdateDetailUseCase;
        this.logoutUserCase = logoutUseCase;
        this.activityPageUseCase = activityPageUseCase;
        this.nfsViewModelDataMapper = nfsViewModelDataMapper;
    }

    @Override
    public void onResume() {
        this.updateActivityPage();
    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        getUpdateDetailUseCase.unsubscribe();
        logoutUserCase.unsubscribe();
        activityPageUseCase.unsubscribe();
    }

    @Override
    public void setView(@NonNull MainView view) {
        this.mainView = view;
    }

    @Override
    public void initialize() {
//        this.checkAppUpdate();
    }

    @Override
    public void checkAppUpdate() {
        this.getUpdateDetailUseCase.execute(new UpdateInfoSubscriber());
    }

    @Override
    public void logout() {
        this.logoutUserCase.execute(new LogoutSubscriber());
    }

    @Override
    public void updateActivityPage() {
        this.activityPageUseCase.execute(new ActivityPageSubscriber());
    }

    private void onDetailInformationReceived(AppUpdate model) {
        final AppUpdateViewModel viewModel = this.nfsViewModelDataMapper.transform(model);
        NfsLog.d(TAG, "onDetailInformationReceived");
        NfsLog.d(TAG, viewModel.toString());
        saveUpdateInfo(viewModel);

        //TODO: need delete below line
//        handleShowAppUpdateInfoEvent(viewModel);

        if (PackageUtils.hasNewVersionApp(this.mainView.getContext(), viewModel.getVersionCode())) {
            NfsLog.d(TAG, "hasNewVersionApp");
            if (AppUpdatePreferencesHelper.allowPromptAppUpdate()) {
                NfsLog.d(TAG, "allowPromptAppUpdate");
                handleShowAppUpdateInfoEvent(viewModel);
            }
        } else {
            resetForceUpdateSwitch();
        }
    }

    private void resetForceUpdateSwitch() {
        AppUpdatePreferencesHelper.setAppNeedForceUpdate(false);
    }

    private void handleShowAppUpdateInfoEvent(AppUpdateViewModel appUpdateViewModel) {
        this.mainView.showAppUpdateView(appUpdateViewModel);
        AppUpdatePreferencesHelper.setLastAppUpdatePromptDay(System.currentTimeMillis());
    }

    private void saveUpdateInfo(AppUpdateViewModel appUpdateViewModel) {
        AppUpdatePreferencesHelper.setAppNeedForceUpdate(appUpdateViewModel.isForceUpdate());
        AppUpdatePreferencesHelper.saveLatestApkUrl(appUpdateViewModel.getApkUrl());
        AppUpdatePreferencesHelper.saveLatestAppVersionCode(appUpdateViewModel.getVersionCode());
    }

    private void onLogoutFinish() {

    }

    private void onActivityPageInfoReceived(ActivityPage model) {
        AdvertisementPreferencesHelper.setActivityIconUrl(model.getPageUrl());
        AdvertisementPreferencesHelper.setActivityPageTitle(model.getPageTitle());
        AdvertisementPreferencesHelper.setActivityPageUrl(model.getPageUrl());
        this.mainView.changeActivityPageIcon(model.getIconUrl());
    }

    private final class UpdateInfoSubscriber extends DefaultSubscriber<AppUpdate> {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onNext(AppUpdate model) {
            MainPresenterImpl.this.onDetailInformationReceived(model);
        }
    }

    private final class LogoutSubscriber extends DefaultSubscriber<Logout> {

        @Override
        public void onCompleted() {
            super.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            e.printStackTrace();
            onLogoutFinish();
        }

        @Override
        public void onNext(Logout model) {
            super.onNext(model);
            NfsLog.d(TAG, model.toString());
            MainPresenterImpl.this.onLogoutFinish();
        }
    }

    private final class ActivityPageSubscriber extends DefaultSubscriber<ActivityPage> {

        @Override
        public void onCompleted() {
            super.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            e.printStackTrace();
            onLogoutFinish();
        }

        @Override
        public void onNext(ActivityPage model) {
            super.onNext(model);
            NfsLog.d(TAG, model.toString());
            MainPresenterImpl.this.onActivityPageInfoReceived(model);
        }
    }
}
